package com.dreamtour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamtour.entity.Users;
import com.dreamtour.mapper.UsersMapper;
import com.dreamtour.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xls
 * @since 2024-12-24
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public int countLoginsBetween(LocalDateTime startTime, LocalDateTime endTime) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("lastLogtime", startTime, endTime);
        return (int) count(queryWrapper);
    }


    @Override
    public List<Map<String, Object>> countUsersByMonth() {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(*) as count, DATE_FORMAT(lastLogtime, '%m') as month");
        queryWrapper.groupBy("month");
        queryWrapper.orderByDesc("month");
        return usersMapper.selectMaps(queryWrapper);
    }

    @Override
    public List<Map<String, Object>> getDailyRegistrations(){
        return usersMapper.getDailyRegistrations();
    }

    @Override
    public List<Map<String, Object>> getAgeDistribution(){
        return usersMapper.getAgeDistribution();
    }

}
